package org.putrpctorest.service.impl.copy;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.putrpctorest.resource.Resource;

public class ResourceImpl {
    // implements Resource {

    private String id;
    private String path;
    private Set<String> produces = new HashSet<String>();
    private Set<String> consumes = new HashSet<String>();
    private Set<MethodImpl> methods = new HashSet<MethodImpl>();
    private ServicesImpl restServices;

    public void setId(String id) {
        this.id = id;
    }

    // @Override
    public String getId() {
        return id;
    }

    // @Override
    public String getName() {
        return getServicesDefinitions().getId() + "." + getId();
    }

    public void setPath(String path) {
        this.path = path;
    }

    // @Override
    public String getPath() {
        return path;
    }

    public void setProduces(Set<String> mediaTypes) {
        produces = new HashSet<String>(mediaTypes);
    }

    // @Override
    public Set<String> getProduces() {
        Set<String> allProduces = new HashSet<String>();
        allProduces.addAll(produces);
        allProduces.addAll(getServicesDefinitions().getProduces());
        return Collections.unmodifiableSet(allProduces);
    }

    public void addProduces(String mediaType) {
        produces.add(mediaType);
    }

    public void setConsumes(Set<String> mediaTypes) {
        consumes = new HashSet<String>(mediaTypes);
    }

    // @Override
    public Set<String> getConsumes() {
        Set<String> allConsumes = new HashSet<String>();
        allConsumes.addAll(consumes);
        allConsumes.addAll(getServicesDefinitions().getConsumes());
        return Collections.unmodifiableSet(allConsumes);
    }

    public void addConsumes(String mediaType) {
        consumes.add(mediaType);
    }

    public void setMethods(Set<MethodImpl> methods) {
        this.methods = new HashSet<MethodImpl>();
        for (MethodImpl method : methods) {
            addMethod(method);
        }
    }

    // @Override
    public Set<MethodImpl> getMethods() {
        return Collections.unmodifiableSet(methods);
    }

    // @Override
    public MethodImpl getMethod(String id) {
        for (MethodImpl method : getMethods()) {
            // if (method.getId().equals(id)) {
            // return method;
            // }
        }
        return null;
    }

    public void addMethod(MethodImpl method) {
        if (method.getResource() == null) {
            method.setResource(this);
        }
        methods.add(method);
    }

    // @Override
    public ServicesImpl getServicesDefinitions() {
        return restServices;
    }

    public void setServicesDefinitions(ServicesImpl restServices) {
        this.restServices = restServices;
        if (restServices.getResource(getId()) == null) {
            restServices.addResource(this);
        }
    }

    public String toString() {
        return "[Resource globalId=\"" + getName() + "]";
    }

    public boolean equals(Object o) {
        if (getId() != null) {
            if (o instanceof Resource) {
                if (getId().equals(((Resource) o).getId())) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            if (((Resource) o).getId() == null) {
                return true;
            } else {
                return false;
            }
        }
        return true;

    }

}
